package com.hulu.yec.application.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hulu.yec.common.Constant;
import com.hulu.yec.sys.entity.MenuEntity;
import com.hulu.yec.sys.service.SysDao;
import com.hulu.yec.sys.user.entity.SysUserEntity;

/**
 * 项目公共的控制器
 * 
 * @author hf 2016年3月7日
 */
@WebServlet("/ApplicationServlet")
public class ApplicationServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding(Constant.ENCODING_UTF8);
		String command = request.getParameter("command");
		if ("top".equals(command)) {
			top(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("center".equals(command)) {
			center(request, response);
		} else if ("down".equals(command)) {
			down(request, response);
		} else if ("left".equals(command)) {
			left(request, response);
		} else if ("right".equals(command)) {
			right(request, response);
		} else {
			request.getRequestDispatcher("/WEB-INF/view/common/404.jsp").forward(request, response);
		}
	}

	
	/**
	 * 到top.jsp
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void top(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/view/sys/top.jsp").forward(request, response);
	}

	/**
	 * 到left.jsp
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void left(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute("user");
		List<MenuEntity> mainMenuList = new SysDao().getMainMenu(user.getId()) ;
		List<MenuEntity> childMenuList = new SysDao().getChildMenu(user.getId()) ;
		request.setAttribute("mainMenuList",mainMenuList);
		request.setAttribute("childMenuList",childMenuList);
		request.getRequestDispatcher("/WEB-INF/view/sys/left.jsp").forward(request, response);
	}

	/**
	 * 到right.jsp
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void right(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/view/sys/right.jsp").forward(request, response);
	}

	/**
	 * 到main.jsp
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/view/sys/main.jsp").forward(request, response);
	}

	/**
	 * 到center.jsp
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void center(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/view/sys/center.jsp").forward(request, response);
	}

	/**
	 * 到down.jsp
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void down(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/view/sys/down.jsp").forward(request, response);
	}
}
